1
|
|
|
"use babel"; |
2
|
|
|
|
3
|
|
|
import DevmindBeautifierView from "./devmind-beautifier-view"; |
4
|
|
|
import { CompositeDisposable } from "atom"; |
5
|
|
|
var qs = require("qs"); |
6
|
|
|
|
7
|
|
|
function request(ext, text, url) { |
8
|
|
|
data = { extension: ext, content: text }; |
|
|
|
|
9
|
|
|
var http = new XMLHttpRequest(); |
|
|
|
|
10
|
|
|
http.open("POST", url, true); |
11
|
|
|
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
12
|
|
|
http.send(qs.stringify(data)); |
13
|
|
|
changeState(http); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
function changeState(http) { |
17
|
|
|
http.onreadystatechange = function() { |
18
|
|
|
if (http.readyState == 4 && http.status == 200) { |
19
|
|
|
success(http); |
20
|
|
|
} |
21
|
|
|
if (http.status !== 200) { |
22
|
|
|
fail(); |
23
|
|
|
} |
24
|
|
|
}; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
function success(http) { |
28
|
|
|
if (typeof http.responseText.status === "undefined") { |
29
|
|
|
editor.setText(http.responseText); |
|
|
|
|
30
|
|
|
atom.notifications.addSuccess("Now your code looks fancy"); |
|
|
|
|
31
|
|
|
panel.modalPanel.hide(); |
|
|
|
|
32
|
|
|
} else { |
33
|
|
|
atom.notifications.addInfo("File extension not supported. Yet."); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
function fail() { |
38
|
|
|
panel.modalPanel.hide(); |
|
|
|
|
39
|
|
|
atom.notifications.addError( |
|
|
|
|
40
|
|
|
"Something went wrong, please check your internet connection" |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
export default { |
45
|
|
|
devmindBeautifierView: null, |
46
|
|
|
modalPanel: null, |
47
|
|
|
subscriptions: null, |
48
|
|
|
|
49
|
|
|
activate(state) { |
50
|
|
|
this.devmindBeautifierView = new DevmindBeautifierView( |
51
|
|
|
state.devmindBeautifierViewState |
52
|
|
|
); |
53
|
|
|
this.modalPanel = atom.workspace.addModalPanel({ |
|
|
|
|
54
|
|
|
item: this.devmindBeautifierView.getElement(), |
55
|
|
|
visible: false |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
// Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable |
59
|
|
|
this.subscriptions = new CompositeDisposable(); |
60
|
|
|
|
61
|
|
|
// Register command that toggles this view |
62
|
|
|
this.subscriptions.add( |
63
|
|
|
atom.commands.add("atom-workspace", { |
64
|
|
|
"devmind-beautifier:toggle": () => this.toggle() |
65
|
|
|
}) |
66
|
|
|
); |
67
|
|
|
}, |
68
|
|
|
|
69
|
|
|
deactivate() { |
70
|
|
|
this.modalPanel.destroy(); |
71
|
|
|
this.subscriptions.dispose(); |
72
|
|
|
this.devmindBeautifierView.destroy(); |
73
|
|
|
}, |
74
|
|
|
|
75
|
|
|
serialize() { |
76
|
|
|
return { |
77
|
|
|
devmindBeautifierViewState: this.devmindBeautifierView.serialize() |
78
|
|
|
}; |
79
|
|
|
}, |
80
|
|
|
|
81
|
|
|
toggle() { |
82
|
|
|
panel = this; |
|
|
|
|
83
|
|
|
|
84
|
|
|
editor = atom.workspace.getActiveTextEditor(); |
|
|
|
|
85
|
|
|
text = editor.getText().trim(); |
|
|
|
|
86
|
|
|
fileName = editor.getTitle(); |
|
|
|
|
87
|
|
|
ext = fileName |
|
|
|
|
88
|
|
|
.split(".") |
89
|
|
|
.pop() |
90
|
|
|
.trim(); |
91
|
|
|
request(ext, text, "https://beautifier.devmind.io/format"); |
92
|
|
|
} |
93
|
|
|
}; |
94
|
|
|
|